﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Gnome.Entities;
using FlatRedBall;

namespace Gnome.Engine
{
    public static class ItemFactory
    {

        private static float resizeFactor = 0.8f;
        //This list stores each item and its associated probablility as a key-value pair in a single-item dictionary.
        //This is stored as a list of single-item dictionaries, rather than a single, multiple-item dictionary, to
        //ensure that each time the list is traversed, the items are searched in the same order. The order of items
        //in a dictionary is not set, and this could interfere with the probabilities.
        private static readonly List<Dictionary<Item.VariableState, int>> probabilities = new List<Dictionary<Item.VariableState, int>>
        {
            {new Dictionary<Item.VariableState, int> {{Item.VariableState.Common1, 25}}},
            {new Dictionary<Item.VariableState, int> {{Item.VariableState.Common2, 25}}},
            {new Dictionary<Item.VariableState, int> {{Item.VariableState.Uncommon, 25}}},
            {new Dictionary<Item.VariableState, int> {{Item.VariableState.Rare, 15}}},
            {new Dictionary<Item.VariableState, int> {{Item.VariableState.Hidden, 10}}}
        };

    
        /*
         * This method is called when a new item is needed. The method chooses an item type
         * at random. Then, based on the chosen type, it sets the object's state, weight, and value
         */
        public static Item get(Vector3 position, String contentManagerName)
        {
            //Select object type at random.
            int value = FlatRedBallServices.Random.Next(1, 101);
            int probability = 0;
            Item.VariableState type = Item.VariableState.Common1;
            foreach (Dictionary<Item.VariableState, int> item in probabilities)
            {
                probability += item.Values.ElementAt(0);
                if (value <= probability)
                {
                    type = item.Keys.ElementAt(0);
                    break;
                }
            }
            //Make a new item based on the given type
            Item returnItem = new Item(contentManagerName, true);
            returnItem.CurrentState = type;
            returnItem.Position = position;
            returnItem.CollisionBox.Position = position;
            returnItem.ItemSpriteScaleX = resizeFactor;
            returnItem.ItemSpriteScaleY = resizeFactor;
            returnItem.CollisionBox.ScaleX = resizeFactor;
            returnItem.CollisionBox.ScaleY = resizeFactor;
            return returnItem;
        }

        /*
         * This version of get is called when an item of a specific type is needed
         * */
        public static Item get(Item.VariableState type, Vector3 position, string ContentManagerName)
        {
            Item returnItem = new Item(ContentManagerName);
            returnItem.CurrentState = type;
            returnItem.Position = position;
            returnItem.CollisionBox.Position = position;
            returnItem.ItemSpriteScaleX = resizeFactor;
            returnItem.ItemSpriteScaleY = resizeFactor;
            returnItem.CollisionBox.ScaleX = resizeFactor;
            returnItem.CollisionBox.ScaleY = resizeFactor;
            return returnItem;
        }
    }
}
